
#ifndef CT_UARTV2_H
#define CT_UARTV2_H

#include "ct_ringbuffer.h"

// Enumeration for UART port numbers
typedef enum {
    CT_UART0_TIMER1 = (uint8_t)1,
    CT_UART0_TIMER3 = (uint8_t)2,
    CT_UART1_TIMER3 = (uint8_t)3,
    CT_UART2        = (uint8_t)4,
    CT_UART3        = (uint8_t)5,
    CT_UART0        = (uint8_t)6,
    CT_UART1        = (uint8_t)7
} ct_uart_port_t;

// Error codes for UART initialization
typedef enum {
    CT_UART_SUCCESS = 0,
    CT_UART_ERROR_NULL_POINTER,
    CT_UART_ERROR_INVALID_ID,
    // More error codes can be added here
} ct_uart_error_t;

typedef struct {
    volatile ct_uart_port_t id;                  // UART port number
    volatile RingBuffer_t   rx_ringbuffer;       // Ring buffer for receive data
    volatile RingBuffer_t   tx_ringbuffer;       // Ring buffer for transmit data
    volatile uint8_t        counter;             // Counter for transmit data
    volatile uint8_t        rx_timeout;          // Receive timeout count
    uint8_t        rx_bTimeout : 1;     // Receive timeout flag
    uint8_t        rx_bOverflow : 1;    // Receive overflow flag
    uint8_t        tx_bBusy : 1;        // Transmit busy status flag
} UART_t;

extern UART_t g_Uart0;

void ct_uart_Init(UART_t        *UARTx,
                  ct_uart_port_t id,
                  uint32_t       baudrate,
                  int8_t        *rx_buffer_space,
                  uint16_t        rx_buffer_size,
                  int8_t        *tx_buffer_space,
                  uint16_t        tx_buffer_size);
void    ct_uart_IntReceiveBuff(UART_t *UARTx);
void    ct_uart_SendData(UART_t *UARTx, uint8_t dat);
void    ct_uart_SendBuff(UART_t *UARTx, uint8_t *buff, uint8_t len);
void    ct_uart_ResetIntSendFlag(UART_t *UARTx);
int8_t  ct_uart_PutBuff(UART_t *UARTx, uint8_t *buff, uint8_t len);
void    ct_uart_Transmission(UART_t *UARTx);
void    ct_uart_IntTransmission(UART_t *UARTx);
void    ct_uart_SendString(UART_t *UARTx, uint8_t *str);
uint8_t ct_uart_ReceiveData(UART_t *UARTx);
void    ct_uart_InterruptEnable(ct_uart_port_t id, uint8_t Status);
void    ControlUARTInterrupts(UART_t *UARTx, unsigned char enable);

#endif /* CT_UART_H */